package chemicalDiagramsHelp;

/** a UI subclass that will open external links (website or ftp links) in an external browser
 * (written? by "hlegend"; see "http://forums.sun.com/thread.jspa?threadID=728061"  */
public class ExternalLinkContentViewerUI extends javax.help.plaf.basic.BasicContentViewerUI {
  public ExternalLinkContentViewerUI(javax.help.JHelpContentViewer x) {
      super(x);
  } // constructor

  public static javax.swing.plaf.ComponentUI createUI(javax.swing.JComponent x) {
      return new ExternalLinkContentViewerUI((javax.help.JHelpContentViewer)x);
  } // createUI

  @Override
  public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent he){
      if(he.getEventType()== javax.swing.event.HyperlinkEvent.EventType.ACTIVATED){
          try{
              final java.net.URL u = he.getURL();
              if(u != null) {
                  if(u.getProtocol().equalsIgnoreCase("http")
                          ||u.getProtocol().equalsIgnoreCase("ftp")) {
                      Thread t = new Thread() {@Override public void run(){
                        //java.awt.Desktop.browse(u);
                        BareBonesBrowserLaunch.openURL(u.toString());
                      }};
                      t.start(); // Note: t.start() returns inmediately.
                      return;
                  }
              }
          } catch(Throwable t){}
      } // if(eventType.ACTIVATED)
      super.hyperlinkUpdate(he);
  } // hyperlinkUpdate

  //<editor-fold defaultstate="collapsed" desc="class BareBonesBrowserLaunch">
  /////////////////////////////////////////////////////////
  //  Bare Bones Browser Launch                          //
  //  Version 2.0 (May 26, 2009)                         //
  //  By Dem Pilafian                                    //
  //  Supports:                                          //
  //     Mac OS X, GNU/Linux, Unix, Windows XP/Vista     //
  //  Example Usage:                                     //
  //     String url = "http://www.centerkey.com/";       //
  //     BareBonesBrowserLaunch.openURL(url);            //
  //  Public Domain Software -- Free to Use as You Like  //
  /////////////////////////////////////////////////////////
  /**
   * <b>Bare Bones Browser Launch for Java</b><br>
   * Utility class to open a web page from a Swing application
   * in the user's default browser.<br>
   * Supports: Mac OS X, GNU/Linux, Unix, Windows XP/Vista<br>
   * Example Usage:<code><br> &nbsp; &nbsp;
   *    String url = "http://www.google.com/";<br> &nbsp; &nbsp;
   *    BareBonesBrowserLaunch.openURL(url);<br></code>
   * Latest Version: <a href="http://www.centerkey.com/java/browser/">www.centerkey.com/java/browser</a><br>
   * Author: Dem Pilafian<br>
   * Public Domain Software -- Free to Use as You Like
   * @version 2.0, May 26, 2009 */
  private static class BareBonesBrowserLaunch {
  private static final String nl = System.getProperty("line.separator");
  private static final String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
      "seamonkey", "galeon", "kazehakase", "mozilla", "netscape", "chrome" };
  /**  Opens the specified web page in a web browser
   * @param url A web address (URL) of a web page (ex: "http://www.google.com/")
   */
  private static void openURL(String url) {
      String osName = System.getProperty("os.name");
      try {
          if(osName.startsWith("Mac OS")) {
              Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
              java.lang.reflect.Method openURL = fileMgr.getDeclaredMethod("openURL",
                  new Class[] {String.class});
              openURL.invoke(null, new Object[] {url});
              }
          else if(osName.startsWith("Windows"))
              Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url).waitFor();
          else {//assume Unix or Linux
              boolean found = false;
              for(String browser : browsers)
                  if(!found) {
                      found = Runtime.getRuntime().exec(new String[] {"which", browser}).waitFor() == 0;
                      if(found) {Runtime.getRuntime().exec(new String[] {browser, url});}
                  }
              if(!found) {throw new Exception(java.util.Arrays.toString(browsers));}
              }// Unix or Linux
          }
      catch (Exception e) {
          String msg = "Error attempting to launch web browser"+nl+e.toString();
          System.err.println("BareBonesBrowserLaunch - "+msg);
          javax.swing.JOptionPane.showMessageDialog(null,msg,
              "Chemical Diagrams Help", javax.swing.JOptionPane.ERROR_MESSAGE);
          }
      } // openURL
  } // class BareBonesBrowserLaunch

  //</editor-fold>

}
